/**
 *
 * User: zeng/704729872@qq.com
 * Date: 2018/4/4
 * Time: 15:32
 */

var ulWidth = 0;
var liOWidth = 0;
var parentDiv;
var ulEl;
var liArr;
var currnetSum = 0; // 当前是第几张，默认1
var bannerShow = document.getElementById('banner-img');

_initSlider();
detail_slider(0, false);

function _initSlider() {
    parentDiv = document.getElementsByClassName('show_width')[0];
    ulEl = document.querySelector('#slider-ul'); // 1.获取图片容器为其设置宽度 -> li.widht * li.lenght
    liArr = ulEl.getElementsByTagName('li'); // 2.获取图片列表

    /* 3.初始化 */
    for (let i = 0; i < liArr.length; i++) {
        ulWidth += liArr[i].offsetWidth;
    }
    liOWidth = ulWidth / liArr.length;
    ulEl.style.width = ulWidth;
}

/**
 *
 * @param n 图片+-数或当前数
 * @param flag 是否累加
 */
function detail_slider(n, flag) { // 0

    /* 是否是直接点击图片切换 */
    if (flag) {
        currnetSum += n;
        n = currnetSum - n < 0 ? 0 : currnetSum - n;
        liArr[n].className = liArr[n].className.replace('active', '');
    } else {
        liArr[currnetSum].className = liArr[currnetSum].className.replace('active', '');
        currnetSum = n;
    }

    /* 判断当前图片索引是否合法 */
    if (currnetSum > liArr.length - 1) {
        currnetSum = liArr.length - 1;
    } else if (currnetSum < 0) {
        currnetSum = 0;
    }

    /* 为当前图片设置不透明 */
    // if (liArr[currnetSum].className.indexOf('active') < 0) {
        liArr[currnetSum].className += ' active';
    // }

    /* 如果当前页大于2，为当前图片设置居中 */
    if (currnetSum >= 1 && currnetSum !== liArr.length - 1) {
        ulEl.style.left = (1 - currnetSum) * (liOWidth) + 'px';
    }

    /* 设置banner显示图片 */

    bannerShow.src = liArr[currnetSum].querySelector('img').src.split('?')[0]+'?imageView2/1/w/700/h/421';
   // alert(bannerShow.src);
}
function validateSC(url) {
    url = (url == undefined ? '/house/docollection' : url);
    console.log(url);
    var data = {'houseId': houseId};
    $.ajax({
        type: "POST",
        url: url,
        data: data,
        dataType: "json",
        success: function (data) {
            console.log(data.data);
            if (data.code == 200) {
                scCSS(data.data,2);
            } else if (data.code == -1) {
                //alert('未登录');
                $('.hb').show();
            }

        }
    });
}
function scCSS(flag,i){
    var _this = $('.shouc-btn');
    if (flag) {
        if(i==2){
            $('.success').text("该房源收藏成功");
            $('.schb').show();
        }
        _this.css({'background': 'rgb(243, 159, 13)'}).css('color', '#fff').css('border', '1px solid rgb(255,255,255)');
        _this.hover(function () {
            _this.css('background', '#c67f0d').css('color', '#fff').css('border', '1px solid rgb(255,255,255)');
        }, function () {
            _this.css('background', '#f39f0d').css('color', '#fff').css('border', '1px solid rgb(255,255,255)');
        });
    } else {
        if(i==2) {
            $('.success').text("该房源取消收藏成功");
            $('.schb').show();
        }
        _this.css({'background': '#fff'});
        _this.css({'color': '#000'});
        _this.css({'border': '1px solid #666666'});
        _this.hover(function () {
            _this.css('background', '#f39f0d').css('color', '#fff').css('border', '1px solid #fff');
        }, function () {
            _this.css({'background': '#fff'}).css({'color': '#000'}).css({'border': '1px solid #666666'});
        });
    }
}

function todetail(to) {
    window.location.href = "/detail?houseId=" + to + "&type=" + dtype;
}
/* 关闭 */
var lgEl = document.getElementsByClassName('hb')[0];
lgEl.addEventListener('click', function (event) {
    if (event.target !== event.currentTarget) return;
    lgEl.style.display = 'none';
}, false)

$('.close').click(function(){
    $('.schb').hide();
});
$(function(){
    scCSS(scFlag,1);
})